<template>
  <div :class="{ 'hidden': hidden }" ref="pageRef" class="pagination-container justifyBetween flex alignCenter">
    <div class="select-num" :class="{ 'hidden': props.selectNum == 0 }">已选 {{props.selectNum}} 项</div>
    <div class="flex alignCenter">
      <div class="total fz14" v-if="props.totalPrice">金额合计:
        <span>{{proxy.$utils.formatNumber(props.totalPrice)}}</span>元
      </div>
      <el-pagination size="small" :background="background" v-model:current-page="currentPage" v-model:page-size="pageSize" :layout="layout" :page-sizes="pageSizes" :pager-count="pagerCount" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script setup>
import { scrollTo } from '@/utils/scroll-to'
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();

const props = defineProps({
  selectNum: {
    type: Number,
    default: 0
  },
  total: {
    required: true,
    type: Number
  },
  page: {
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 20
  },
  pageSizes: {
    type: Array,
    default() {
      return [50, 100, 500, 1000, 5000]
    }
  },
  // 移动端页码按钮的数量端默认值5
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  },
  totalPrice: {
    type: Number,
    default: 0
  }
})

const emit = defineEmits();
const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('update:page', val)
  }
})
const pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    emit('update:limit', val)
  }
})


onMounted(() => {
  nextTick(() => {
    let pageHeight = proxy.$refs.pageRef.offsetHeight;
    console.log("pageHeight", pageHeight)
    counter.pageHeight = pageHeight + 25;
  });
})

function handleSizeChange(val) {
  if (currentPage.value * val > props.total) {
    currentPage.value = 1
  }
  emit('pagination', { page: currentPage.value, limit: val })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}
function handleCurrentChange(val) {
  emit('pagination', { page: val, limit: pageSize.value })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}

</script>

<style lang="scss" scoped>
.pagination-container {
  background: #fff;
  margin: 25px !important;
  padding: 0 !important;
  display: flex;
  .el-pagination {
    position: initial;
  }
}
.select-num {
  font-weight: 400;
  color: var(--el-text-color-regular);
  font-size: 14px;
}
.pagination-container.hidden {
  display: none;
}
.hidden {
  opacity: 0;
}
.total {
  margin-right: 25px;
  span {
    color: var(--el-color-primary);
    font-weight: bold;
    font-size: 16px;
    padding-right: 3px;
  }
}
</style>